package cn.broadin.app.hotel;

import cn.broadin.app.hotel.config.WiFiAPListener;
import cn.broadin.app.hotel.config.WiFiAPService;
import cn.broadin.app.hotel.config.WifiHotUtil;
import cn.broadin.app.hotel.util.UtilFile;
import cn.broadin.app.hotel.util.XToast;
import cn.broadin.app.hotel.view.StrokeImageButton;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;


public class HotsportConfig extends Activity {
	protected static final String TAG = "HotsportConfig";
	EditText name;
	EditText password;
	ProgressDialog progressDialog = null;
	String HotPassword,HotName;
	String HotStatus;
	public static String result = "";
	public String auto_openFlag = "false";
	private boolean isWifiOpen = false;
	private WifiHotUtil wifiHotUtil;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wifi);
		
		//获取热点的开关状态
		/*Intent intent=getIntent();  
		Bundle bundle = intent.getExtras();
		HotStatus = bundle.getString("HotStatus");
		if(HotStatus.equals("on")) {
			XToast.makeText(HotsportConfig.this, "热点处于开启状态,如需重置账号密码,请先关闭热点").show();
		}*/
		
		name = (EditText) findViewById(R.id.Hotname);
		name.setInputType(InputType.TYPE_CLASS_TEXT);
		WiFiAPService.startService(this);
		wifiHotUtil = new WifiHotUtil(this);
		
		
		
		StrokeImageButton button = (StrokeImageButton)findViewById(R.id.Auto_Open);
		button.setDrawRect(new Rect(0, 0, 110, 50));
		button.setText("开启热点", 25);
		button.setOnClickListener(fOnclick);

		button = (StrokeImageButton)findViewById(R.id.shutdown);
		button.setDrawRect(new Rect(0, 0, 110, 50));
		button.setText("关闭热点", 25);
		button.setOnClickListener(fOnclick);
		
		button = (StrokeImageButton)findViewById(R.id.cancle);
		button.setDrawRect(new Rect(0, 0, 110, 50));
		button.setText("退出", 25);
		button.setOnClickListener(fOnclick);
		
		password = (EditText)findViewById(R.id.HotPassword);
		
		/*ArrayList<String> macStr = UtilTool.getLocalMacAddress(this);
		if(macStr.size() > 0){
			macAddress = macStr.get(0);
			mac.setText(macAddress);
		}*/
		
		password.setInputType(InputType.TYPE_CLASS_TEXT);
		progressDialog = new ProgressDialog(this);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		
		WiFiAPService.addWiFiAPListener(new WiFiAPListener() {
			
			@Override
			public void stateChanged(int state) {
				Log.i(TAG, "dyh-wifiHotstate= "+state);
                switch (state) {
				case WiFiAPListener.WIFI_AP_OPENING:
					isWifiOpen = true;
					break;
				case WiFiAPListener.WIFI_AP_OPEN_SUCCESS:
					isWifiOpen = true;				
					break;
				case WiFiAPListener.WIFI_AP_CLOSEING:
					isWifiOpen = false;
					break;
				case WiFiAPListener.WIFI_AP_CLOSE_SUCCESS:
					isWifiOpen = false;
					break;
				default:
					break;
				}
			}
		});
		
	}
	
	
	
	
	OnClickListener fOnclick = new OnClickListener(){

		@Override
		public void onClick(View view) {
			switch(view.getId()){
			case R.id.Auto_Open:
				progressDialog.setMessage("正在打开热点!");
				progressDialog.show();
				auto_openFlag = "true";
				HotSpotConfig();
				break;
			case R.id.shutdown:
				progressDialog.setMessage("正在关闭热点!");
				progressDialog.show();
				auto_openFlag = "false";
				closeWifiHot();
				//finish();
				break;
			case R.id.cancle:
				progressDialog.hide();
				finish();
				break;
			}
		}
		
	};
	
	
	public void saveConfig(String HotName, String HotPassword, String autoFlag) {
		System.out.println("dyh---- HotPasswordLength = "+HotPassword.length());
		if(HotName.equals("")) {
			mHandler.sendEmptyMessage(3);
		} else {
			if(!HotPassword.equals("") && HotPassword.length() < 8) {
				mHandler.sendEmptyMessage(2);
			} else {
				if ( Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
					String tempContent = HotName + ";" + HotPassword + ";" + autoFlag;
					String dirName = "/data/local/Hotconfig/";
					String fileName = "Hotspot.txt";
					if(UtilFile.saveFile(dirName, fileName, tempContent)) {
						System.out.println("dyh-----openWifiHot--isWifiOpen = "+isWifiOpen);
						if (!isWifiOpen) {
							int tempType = 1; //需要加密；
							if(HotPassword.equals("")) {
								tempType = 0; //无需密码
							} 
							openWifiHot(tempType);
							mHandler.sendEmptyMessage(1);
						}
						
						
					} else {
						mHandler.sendEmptyMessage(0);
					}
				}
			}
		}
		
			
		
	}
	
	
	
	private void openWifiHot(int type) {
		wifiHotUtil.startWifiAp(HotName,HotPassword,type);
	}
	
	private void closeWifiHot() {
		System.out.println("dyh-----closeWifiHot--isWifiOpen = "+isWifiOpen);
		setWifiAutoFlag();		
	}
	
	public void setWifiAutoFlag() {
		String dirName = "/data/local/Hotconfig/";
		String fileName = "Hotspot.txt";
		String tempContent = HotName + ";" + HotPassword + ";" + auto_openFlag;
		wifiHotUtil.closeWifiAp();
		System.out.println("dyh-----closeWifiHot--tempContent = "+tempContent);
		if(UtilFile.saveFile(dirName, fileName, tempContent)){
			mHandler.sendEmptyMessage(4);
		} else {
			mHandler.sendEmptyMessage(0);
		}
		
	}
	
	public void HotSpotConfig() {
		HotName = name.getText().toString().trim();
		HotPassword = password.getText().toString().trim();
		saveConfig(HotName, HotPassword,auto_openFlag);			
	}

	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 0:
				 progressDialog.dismiss();
				 Toast.makeText(HotsportConfig.this, "写入文件错误", Toast.LENGTH_SHORT).show();
			 
				break;
				
			case 1:
				progressDialog.dismiss();
				Toast.makeText(HotsportConfig.this, "热点开启成功", Toast.LENGTH_SHORT).show();				
				break;			
			case 2:
				progressDialog.dismiss();
				Toast.makeText(HotsportConfig.this, "密码长度须大于8位", Toast.LENGTH_SHORT).show();
				break;
			case 3:
				progressDialog.dismiss();
				Toast.makeText(HotsportConfig.this, "热点名称不能为空", Toast.LENGTH_SHORT).show();
				break;
			case 4:
				progressDialog.dismiss();
				Toast.makeText(HotsportConfig.this, "热点关闭成功", Toast.LENGTH_SHORT).show();	
				break;
			}
			//finish();
			super.handleMessage(msg);
		}
	};
	
}

